home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / FLTK-1.0.6 / src / Fl_Positioner.cxx < prev    next >
Encoding:
C/C++ Source or Header  |  1999-01-07  |  3.6 KB  |  134 lines

  1. //
  2. // "$Id: Fl_Positioner.cxx,v 1.4 1999/01/07 19:17:25 mike Exp $"
  3. //
  4. // Positioner widget for the Fast Light Tool Kit (FLTK).
  5. //
  6. // Copyright 1998-1999 by Bill Spitzak and others.
  7. //
  8. // This library is free software; you can redistribute it and/or
  9. // modify it under the terms of the GNU Library General Public
  10. // License as published by the Free Software Foundation; either
  11. // version 2 of the License, or (at your option) any later version.
  12. //
  13. // This library is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16. // Library General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Library General Public
  19. // License along with this library; if not, write to the Free Software
  20. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  21. // USA.
  22. //
  23. // Please report all bugs and problems to "fltk-bugs@easysw.com".
  24. //
  25.  
  26. // The positioner widget from Forms, gives 2D input
  27. // Written by: Mark Overmars
  28.  
  29. #include <FL/Fl.H>
  30. #include <FL/Fl_Positioner.H>
  31. #include <FL/fl_draw.H>
  32.  
  33. static double flinear(double val, double smin, double smax, double gmin, double gmax)
  34. {
  35.   if (smin == smax) return gmax;
  36.   else return gmin + (gmax - gmin) * (val - smin) / (smax - smin);
  37. }
  38.  
  39. void Fl_Positioner::draw(int x, int y, int w, int h) {
  40.   int x1 = x + 4;
  41.   int y1 = y + 4;
  42.   int w1 = w - 2 * 4;
  43.   int h1 = h - 2 * 4;
  44.   int xx = int(flinear(xvalue(), xmin, xmax, x1, x1+w1-1)+.5);
  45.   int yy = int(flinear(yvalue(), ymin, ymax, y1, y1+h1-1)+.5);
  46.   draw_box(box(), x, y, w, h, color());
  47.   fl_color(selection_color());
  48.   fl_xyline(x1, yy, x1+w1);
  49.   fl_yxline(xx, y1, y1+h1);
  50. }
  51.  
  52. void Fl_Positioner::draw() {
  53.   draw(x(), y(), w(), h());
  54.   draw_label();
  55. }
  56.  
  57. int Fl_Positioner::value(double X, double Y) {
  58.   clear_changed();
  59.   if (X == xvalue_ && Y == yvalue_) return 0;
  60.   xvalue_ = X; yvalue_ = Y;
  61.   redraw();
  62.   return 1;
  63. }
  64.  
  65. int Fl_Positioner::xvalue(double X) {
  66.   return(value(X, yvalue_));
  67. }
  68.  
  69. int Fl_Positioner::yvalue(double Y) {
  70.   return(value(xvalue_, Y));
  71. }
  72.  
  73. int Fl_Positioner::handle(int event, int x, int y, int w, int h) {
  74.   switch (event) {
  75.   case FL_PUSH:
  76.   case FL_DRAG:
  77.   case FL_RELEASE: {
  78.     double x1 = x + 4;
  79.     double y1 = y + 4;
  80.     double w1 = w - 2 * 4;
  81.     double h1 = h - 2 * 4;
  82.     double X = flinear(Fl::event_x(), x1, x1+w1-1.0, xmin, xmax);
  83.     if (xstep_) X = int(X/xstep_+0.5) * xstep_;
  84.     if (X < xmin) X = xmin;
  85.     if (X > xmax) X = xmax;
  86.     double Y = flinear(Fl::event_y(), y1, y1+h1-1.0, ymin, ymax);
  87.     if (ystep_) Y = int(Y/ystep_+0.5) * ystep_;
  88.     if (Y < ymin) Y = ymin;
  89.     if (Y > ymax) Y = ymax;
  90.     if (value(X, Y)) set_changed();}
  91.     if (!(when() & FL_WHEN_CHANGED ||
  92.       when() & FL_WHEN_RELEASE && event == FL_RELEASE)) return 1;
  93.     if (changed() || when()&FL_WHEN_NOT_CHANGED) {
  94.       clear_changed(); do_callback();}
  95.     return 1;
  96.   default:
  97.     return 0;
  98.   }
  99. }
  100.  
  101. int Fl_Positioner::handle(int e) {
  102.   return handle(e, x(), y(), w(), h());
  103. }
  104.  
  105. Fl_Positioner::Fl_Positioner(int x, int y, int w, int h, const char* l)
  106. : Fl_Widget(x, y, w, h, l) {
  107.   box(FL_DOWN_BOX);
  108.   selection_color(FL_RED);
  109.   align(FL_ALIGN_BOTTOM);
  110.   when(FL_WHEN_CHANGED);
  111.   xmin = ymin = 0;
  112.   xmax = ymax = 1;
  113.   xvalue_ = yvalue_ = .5;
  114.   xstep_ = ystep_ = 0;
  115. }
  116.  
  117. void Fl_Positioner::xbounds(double a, double b) {
  118.   if (a != xmin || b != xmax) {
  119.     xmin = a; xmax = b;
  120.     redraw();
  121.   }
  122. }
  123.  
  124. void Fl_Positioner::ybounds(double a, double b) {
  125.   if (a != ymin || b != ymax) {
  126.     ymin = a; ymax = b;
  127.     redraw();
  128.   }
  129. }
  130.  
  131. //
  132. // End of "$Id: Fl_Positioner.cxx,v 1.4 1999/01/07 19:17:25 mike Exp $".
  133. //
  134.